package com.dst.smart.music;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;

import com.dst.smart.app.MyApplication;
import com.dst.smart.constant.Constants;
import com.dst.smart.json.JSONFileUtils;
import com.dst.smart.json.ProjectUtils;
import android.text.TextUtils;

public class NetMusicUitils {
	public static List<NetMusic> list = new ArrayList<NetMusic>();
	private static File jsonFile = new File(MyApplication.getInstance()
			.getRootPath() + File.separator + "net_music_json");
	static {
		if (list.size() == 0) {
			String json = JSONFileUtils.getJsonStringFromFile(jsonFile)
					.replace("\r\n", "");
			if (TextUtils.isEmpty(json)) {
				findMusic();
				save();
			} else {
				try {
					JSONArray jsonArray = new JSONArray(json);

					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject object = jsonArray.getJSONObject(i);
						NetMusic netMusic = new NetMusic();
						netMusic.setMusicName(object.getString("musicName"));
						netMusic.setMusicAuthor(object.getString("musicAuthor"));
						netMusic.setMusicUrl(object.getString("musicUrl"));
						netMusic.setMusicType(object.getInt("musicType"));
						netMusic.setMusicDes(object.getString("musicDes"));
						list.add(netMusic);
					}
				} catch (Exception e) {
					// TODO: handle exceptionus
					findMusic();
					save();
				}
			}
		}
	}

	public static String getString() {
		return JSONFileUtils.getJsonStringFromFile(jsonFile)
				.replace("\r\n", "");
	}

	public static void add(NetMusic netMusic) {

		boolean isNo = true;
		for (NetMusic music : list) {
			if (music.getMusicName().equals(netMusic.getMusicName())) {
				isNo = false;
				break;
			}
		}
		if (isNo == true) {
			list.add(netMusic);
			save();
		}
	}

	public static void save() {
		try {

			JSONArray array = new JSONArray();
			for (NetMusic netMusic : list) {
				JSONObject bject = new JSONObject();
				bject.put("musicName", netMusic.getMusicName());
				bject.put("musicAuthor", netMusic.getMusicAuthor());
				bject.put("musicUrl", netMusic.getMusicUrl());
				bject.put("musicType", netMusic.getMusicType());
				bject.put("musicDes", netMusic.getMusicDes());
				array.put(bject);
			}
			JSONFileUtils.saveJSONToFile(jsonFile, array.toString());
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

	public static void reSetMusic() {
		list.clear();
		findMusic();
		save();
	}

	public static void findMusic() {
		File file = new File(Constants.MUSIC_PATH);
		File[] subFile = file.listFiles();
		if (subFile != null) {

			for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
				// 判断是否为文件夹
				if (!subFile[iFileLength].isDirectory()) {
					String filename = subFile[iFileLength].getName();
					if (filename.trim().toLowerCase().endsWith(".mp3")) {
						try {
							NetMusic nativeMusic = new NetMusic();
							nativeMusic.setMusicName(filename.replace(".mp3",
									""));
							nativeMusic.setMusicAuthor(filename.replace(".mp3",
									""));
							nativeMusic.setMusicDes("请欣�?"
									+ filename.replace(".mp3", ""));
							nativeMusic.setMusicUrl(subFile[iFileLength]
									.getPath());
							nativeMusic.setMusicType(1);
							list.add(nativeMusic);

						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
			}
		}
	}

	public static int current = -1;



}
